//
//  BoyTableViewCell.m
//  UI10_自定义Cell
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "BoyTableViewCell.h"

@implementation BoyTableViewCell

+ (CGFloat)heightWithStr:(NSString *)str    // 封装
{
    CGRect frame = [str boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];
    return frame.size.height;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

#warning 自适应高度2 - 自适应label高度, 注意label宽度应与自适应指定的宽度相同
//  CGRect frame = [self.label.text boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];
    
    // 当.语法 与 访问结构体成员的.混合时, 系统不识别会报错
//    self.label.frame.size.width = frame.size.height;
    CGFloat height = [[self class] heightWithStr:self.label.text];      // 封装
    
    // 先保存label的坐标及宽度信息
    CGRect frameL = self.label.frame;
//    frameL.size.height = frame.size.height;
    frameL.size.height = height;         // 封装
    self.label.frame = frameL;

}

   // 重写set方法传值,有错误...
//- (void)setLabel:(NSString *)content
//{
//    if (content != _content) {
//        [_content release];
//        _content = [content copy];
//    }
//    self.label.text = content;
//    
//    CGRect frame = [content boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];
//    
//    // 当.语法 与 访问结构体成员的.混合时, 系统不识别会报错
//    //    self.label.frame.size.width = frame.size.height;
//    
//    // 先保存label的坐标及宽度信息
//    CGRect frameL = self.label.frame;
//    frameL.size.height = frame.size.height;
//    self.label.frame = frameL;
//}

- (void)dealloc
{
    [_label release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor yellowColor];
    
//    self.contentView.backgroundColor = [UIColor blackColor];
    

//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    self.label.text = @"王宁";
#warning 自适应高度3 - 显示行数设置为自适应(0)
    self.label.numberOfLines = 0;
    self.label.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.label];
//    [self addSubview:label];
    [_label release];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
